package com.nq.controller.backend;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.nq.common.ServerResponse;
import com.nq.pojo.User;
import com.nq.pojo.UserBank;
import com.nq.pojo.vo.UserWithdrawVo;
import com.nq.service.IUserBankService;
import com.nq.service.IUserService;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nq.service.SmsService;
import com.nq.vo.user.UserInfoVO;
import org.apache.poi.ss.usermodel.Workbook;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping({"/admin/user/"})
public class AdminUserController {
    private static final Logger log = LoggerFactory.getLogger(AdminUserController.class);

    @Autowired
    IUserService iUserService;

    @Autowired
    IUserBankService iUserBankService;

    @Autowired
    SmsService smsService;

    @Resource
    SqlSessionTemplate sqlSessionTemplate;

    //分页查询所有用户列表信息 及模糊查询用户信息
    @RequestMapping({"list.do"})
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "realName", required = false) String realName,
                               @RequestParam(value = "phone", required = false) String phone,
                               @RequestParam(value = "email", required = false) String email,
                               @RequestParam(value = "startAmount", required = false) String startAmount,
                               @RequestParam(value = "agentId", required = false) Integer agentId,
                               @RequestParam(value = "userId", required = false) Integer userId,
                               @RequestParam(value = "accountType", required = false) Integer accountType,
                               @RequestParam(value = "isActive", required = false) Integer isActive,
                               @RequestParam(value = "isLock", required = false) Integer isLock,
                               @RequestParam(value = "isRecharge", required = false) Integer isRecharge,
                               @RequestParam(value = "beginRegTime", required = false) String beginRegTime,
                               @RequestParam(value = "endRegTime", required = false) String endRegTime,
                               @RequestParam(value = "beginAuditTime", required = false) String beginAuditTime,
                               @RequestParam(value = "endAuditTime", required = false) String endAuditTime,
                               @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               HttpServletRequest request) {
        return this.iUserService.listByAdmin(userId, beginRegTime, endRegTime, beginAuditTime, endAuditTime, startAmount, realName, phone, email, agentId,
                accountType, pageNum, pageSize, isActive, isLock, isRecharge, request);
    }

    //查询用户信息是否存在
    @RequestMapping({"detail.do"})
    @ResponseBody
    public ServerResponse detail(Integer userId) {
        return this.iUserService.findByUserId(userId);
    }

    @RequestMapping({"updateLock.do"})
    @ResponseBody
    public ServerResponse updateLock(Integer userId) {
        return this.iUserService.updateLock(userId);
    }

    /**
     * 修改用户列表 用户资金手动入款/扣款
     * @param userId
     * @param amt
     * @param direction
     * @param request
     * @return
     */
    @RequestMapping({"updateAmt.do"})
    @ResponseBody
    public ServerResponse updateAmt(Integer userId, String amt, Integer direction, HttpServletRequest request) {
        //amt转Integer
        return this.iUserService.updateAmt(userId, amt, direction, request);
    }

    //修改用户列表 用户信息
    @RequestMapping({"update.do"})
    @ResponseBody
    public ServerResponse update(User user) {
        return this.iUserService.update(user);
    }
    

    //添加用户列表 用户信息
    @RequestMapping({"addSimulatedAccount.do"})
    @ResponseBody
    public ServerResponse addSimulatedAccount(HttpServletRequest request,
                                              @RequestParam(value = "agentId", required = false) Integer agentId,
                                              @RequestParam(value = "code", required = false) String code,
                                              @RequestParam("phone") String phone,
                                              @RequestParam(value = "amt", required = false) String amt,
                                              @RequestParam(value = "accountType", required = false) Integer accountType,
                                              @RequestParam(value = "payPwd", required = false) String payPwd,
                                              @RequestParam(value = "pwd", required = false) String pwd) {
        return this.iUserService.addSimulatedAccount(agentId, phone, code, pwd, payPwd, amt, accountType, request);
    }


    /**
     * 添加用户列表 用户信息
     * @param code
     * @param phone
     * @return
     */
    @RequestMapping({"batchRegisterAccount.do"})
    @ResponseBody
    public ServerResponse batchRegisterAccount(@RequestParam("code") String code,
                                               @RequestParam("phone") String phone,
                                               @RequestParam("pwd") String pwd,
                                               @RequestParam("count") Integer count) {
        return this.iUserService.batchRegisterAccount(code, phone, pwd, count);
    }

    /**
     * 后台实名审核功能
     * @param userId
     * @param state
     * @param authMsg
     * @return
     */
    @RequestMapping({"authByAdmin.do"})
    @ResponseBody
    public ServerResponse authByAdmin(Integer userId, Integer state, String authMsg) {
        ServerResponse response = this.iUserService.authByAdmin(userId, state, authMsg);
        return response;
    }

    //查看指定 用户列表的用户信息
    @RequestMapping({"getBank.do"})
    @ResponseBody
    public ServerResponse getBank(Integer userId) {
        return this.iUserBankService.getBank(userId);
    }

    /**
     * 查询用户上下级
     * @param userId
     * @return
     */
    @RequestMapping({"getSuperiorAndSubordinate.do"})
    @ResponseBody
    public ServerResponse getSuperiorAndSubordinate(Integer userId) {
        return this.iUserService.getSuperiorAndSubordinate(userId);
    }

    @RequestMapping({"updateBank.do"})
    @ResponseBody
    public ServerResponse updateBank(UserBank userBank) {

        int flag = iUserBankService.updateBankByUserId(userBank);
        if (flag > 0) {
            return ServerResponse.createBySuccess(flag);
        }
        return ServerResponse.createByErrorMsg("fail");
    }

    //删除用户列表 用户信息
    @RequestMapping({"delete.do"})
    @ResponseBody
    public ServerResponse delete(Integer userId, HttpServletRequest request) {
        return this.iUserService.delete(userId, request);
    }

    //删除用户银行卡
    @RequestMapping({"deleteBank.do"})
    @ResponseBody
    public ServerResponse deleteBank(Integer userId, HttpServletRequest request) {
        return this.iUserService.deleteBank(userId, request);
    }

    /**
     * 导出用户信息
     * @param realName
     * @param phone
     * @param startAmount
     * @param agentId
     * @param accountType
     * @param isActive
     * @param isLock
     * @param beginRegTime
     * @param endRegTime
     * @param beginAuditTime
     * @param endAuditTime
     * @param pageNum
     * @param pageSize
     * @param request
     * @param response
     */
    @RequestMapping({"export.do"})
    @ResponseBody
    public void export( @RequestParam(value = "realName", required = false) String realName,
                        @RequestParam(value = "phone", required = false) String phone,
                        @RequestParam(value = "email", required = false) String email,
                        @RequestParam(value = "startAmount", required = false) String startAmount,
                        @RequestParam(value = "agentId", required = false) Integer agentId,
                        @RequestParam(value = "userId", required = false) Integer userId,
                        @RequestParam(value = "accountType", required = false) Integer accountType,
                        @RequestParam(value = "isActive", required = false) Integer isActive,
                        @RequestParam(value = "isLock", required = false) Integer isLock,
                        @RequestParam(value = "isRecharge", required = false) Integer isRecharge,
                        @RequestParam(value = "beginRegTime", required = false) String beginRegTime,
                        @RequestParam(value = "endRegTime", required = false) String endRegTime,
                        @RequestParam(value = "beginAuditTime", required = false) String beginAuditTime,
                        @RequestParam(value = "endAuditTime", required = false) String endAuditTime,
                        @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                        HttpServletRequest request, HttpServletResponse response) {
        List<UserInfoVO> userRechargeList = this.iUserService.listByAdminExport(userId, beginRegTime, endRegTime, beginAuditTime, endAuditTime,
                startAmount, realName, phone, email, agentId,
                accountType, pageNum, pageSize, isActive, isLock, isRecharge, request);


        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户信息表","user data"),
                UserInfoVO.class, userRechargeList);
        try {
            ServletOutputStream outputStream = response.getOutputStream();

            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            log.error("Export Cashout Data Failed：",e);
        }
    }
}
